// $Id: physicsinfo.cpp 470 2008-05-27 16:27:22Z agjermun $
// implementation of the physicsinfo class
//
// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "physicsinfo.h"

using namespace std;

/// Constructor
/// Assigns the given object vector to this object
PhysicsInfo::PhysicsInfo(vector<btRigidBody*> &object)
{
	this->object = object;
	world = NULL;
}

/// Destructor
/// Deletes all phyicsobjects and removes them from world if they belong to one
PhysicsInfo::~PhysicsInfo()
{
	vector<btRigidBody*>::iterator itr = object.begin();

	while(itr != object.end())
	{
		if(*itr && (*itr)->getMotionState())
			delete (*itr)->getMotionState();
		if(world != NULL)
			world->removeRigidBody(*itr);
		itr++;
	}
	object.clear();
}

/**
 * Adds the current objects to the given world
 * @param newWorld must be a valid physics world, results undefined if not (read: crash)
*/
void PhysicsInfo::addToWorld(btDynamicsWorld *newWorld)
{
	world = newWorld;
	vector<btRigidBody*>::iterator itr = object.begin();

	while(itr != object.end())
	{
		world->addRigidBody(*itr);
		itr++;
	}
}

/**
 * Removes the current objects from the physics world, if they belong to one
*/
void PhysicsInfo::removeFromWorld()
{
	if(world != NULL)
	{
		vector<btRigidBody*>::iterator itr = object.begin();

		while(itr != object.end())
		{
			world->removeRigidBody(*itr);
			itr++;
		}
	}
}
